package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.user.dto.ApAuthor;
import com.heima.user.dto.AuthDto;
import com.heima.user.dto.WmUser;
import com.heima.user.entity.ApUser;
import com.heima.user.entity.ApUserRealname;
import com.heima.user.feign.ArticleFeign;
import com.heima.user.feign.MediaFeign;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.IApUserRealnameService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.user.service.IApUserService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * APP实名认证信息表 服务实现类
 * </p>
 *
 * @author syl
 * @since 2022-06-27
 */
@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements IApUserRealnameService {

    @Override
    public ResponseResult listByStatus(AuthDto authDto) {
        IPage<ApUserRealname> page = new Page<>(authDto.getPage(), authDto.getSize());
        // 构建查询条件
        LambdaQueryWrapper<ApUserRealname> query = new LambdaQueryWrapper<>();
        // 根据状态进行查询
        if (authDto.getStatus() != null) {
            query.eq(ApUserRealname::getStatus, authDto.getStatus());
        }
        IPage<ApUserRealname> iPage = this.page(page, query);
        // 构建通用的分页响应
        PageResponseResult responseResult = new PageResponseResult(authDto.getPage(), authDto.getSize(),
                iPage.getTotal(), iPage.getRecords());
        return responseResult;
    }
    @Override
    public ResponseResult authFail(AuthDto authDto) {
        ApUserRealname realname = this.getById(authDto.getId());
        realname.setStatus(2);
        realname.setReason(authDto.getMsg());
        this.updateById(realname);
        return ResponseResult.okResult();
    }
    @Autowired
    private IApUserService iApUserService;

    @Autowired
    private MediaFeign mediaFeign;

    @Autowired
    private ArticleFeign articleFeign;
    @Override
    @GlobalTransactional
    public ResponseResult authPass(AuthDto authDto) {
        ApUser apUser = iApUserService.getById(authDto.getId());
        //远程调用自媒体微服务,创建自媒体人
        WmUser wmUser = createWmUser(apUser);
        //远程调用文章微服务,创建文章作者
        ApAuthor apAuthor = creatApAuthor(apUser,wmUser);
        //创建完文章作者后,需要把文章作者的id设置到自媒体用户的ap_author_id属性中
        wmUser.setApAuthorId(apAuthor.getId());
        mediaFeign.updateWmUser(wmUser);
        //修改ap_user_realname用户的认证状态
        ApUserRealname realname = this.getById(authDto.getId());
        realname.setStatus(9);
        this.updateById(realname);
        //修改ap_user用户的认证状态
        apUser.setFlag(1);//0 普通人  1  自媒体人 2  大v
        apUser.setIsIdentityAuthentication(true);
        iApUserService.updateById(apUser);
        return ResponseResult.okResult();
    }

    private ApAuthor creatApAuthor(ApUser apUser, WmUser wmUser) {
        ApAuthor apAuthor = new ApAuthor();
        apAuthor.setName(apUser.getName());
        apAuthor.setType(2);
        apAuthor.setUserId(apUser.getId());
        apAuthor.setWmUserId(wmUser.getId());
        ResponseResult<ApAuthor> authorResponseResult = articleFeign.saveApAuthor(apAuthor);
        if(authorResponseResult.getCode().equals(0)){
            return authorResponseResult.getData();
        }else {
            throw new RuntimeException("远程调用失败");
        }
    }

    private WmUser createWmUser(ApUser apUser) {
      WmUser wmUser = new WmUser();
      wmUser.setApUserId(apUser.getId());
      wmUser.setName(apUser.getName());
      wmUser.setPassword(apUser.getPassword());
      wmUser.setSalt(apUser.getSalt());
      wmUser.setImage(apUser.getImage());
      wmUser.setPhone(apUser.getPhone());
      wmUser.setStatus(9);
      wmUser.setType(0);
      wmUser.setScore(0);
      ResponseResult<WmUser> responseResult = mediaFeign.saveWmUser(wmUser);
      if (responseResult.getCode().equals(0)){
          return responseResult.getData();
      }else {
          throw new RuntimeException("远程调用失败");
      }
    }
}
